package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.controller.admin.SetmealController;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishesMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishesMapper setmealDishesMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());


        Page<SetmealVO> setmealVOS = setmealMapper.page(setmealPageQueryDTO);
        return new PageResult(setmealVOS.getTotal(), setmealVOS.getResult());
    }

    @Override
    public void status(Integer status, Long id) {
        if (status == StatusConstant.ENABLE) {
            List<Long> ids = setmealDishesMapper.findBySetId(id);
            Integer count = dishMapper.findIdStatus(ids);
            if (ids.size()!=count){
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }

        }
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.status(setmeal);

    }
@Transactional
    @Override
    public void add(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.add(setmeal);
    Long id = setmeal.getId();
    List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
    if (setmealDishes!=null && setmealDishes.size()!=0){
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(id);
        });
        setmealDishesMapper.add(setmealDishes);

    }
}

    @Override
    public SetmealVO categoryById(Long id) {
        SetmealVO setmealVO = setmealMapper.categoryById(id);
        List<SetmealDish> setmealDishes =  setmealDishesMapper.findById(id);
        SetmealVO setmealVO1 = new SetmealVO();
        BeanUtils.copyProperties(setmealVO,setmealVO1);
        setmealVO1.setSetmealDishes(setmealDishes);
        return setmealVO1;
    }
    @Transactional
    @Override
    public void deleteAll(List<Long> ids) {
        for (Long id : ids) {
           Setmeal setmeal = setmealMapper.findById(id);
            if (setmeal.getStatus()== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);

            }
        }
        setmealMapper.deleteAll(ids);
        setmealDishesMapper.deleteAll(ids);
    }
@Transactional
    @Override
    public void upDate(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.upDate(setmeal);

    Long dishId = setmeal.getId();
    List<Long> id = new ArrayList<>();
    id.add(dishId);
     setmealDishesMapper.deleteAll(id);
    List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
    setmealDishes.forEach(s->{
        s.setDishId(dishId);
    });
    setmealDishesMapper.add(setmealDishes);
}

    @Override
    public List<Setmeal> list(Setmeal setmeal) {


            List<Setmeal> list = setmealMapper.list(setmeal);

            return list;



    }

    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}